
###########################################################################################
####### Appendix Materials for "Legislative Effectiveness in the American States"
##########################################################################################
### Includes code to replicate all tables/figures in the appendix, in order, EXCEPT:
### *** For Table A4 (and Figure 1), see separate `SLES_analysis_nccppr.R` script 
### *** For all Tables and Figures in the main text, see separate `SLES_analysis_main.R` script 
###########################################################################################
### Originally run using R version 4.0.2 (2020-06-22)

#rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999, digits = 9)

### **********************************************************************
### Set Working Directory to Folder Containing Replication Files and Data
### **********************************************************************
working_directory <- ""

setwd(working_directory)

### Install/Load Packages
# install.packages(c("tidyverse", "stargazer", "huxtable"))
library(tidyverse) # version 1.3.0
library(stargazer) # version 5.2.2
library(huxtable)  # version 5.0.0
library(estimatr)  # version 0.22.0

### Create Figures Directory in Working Directory if Not Present
if(!dir.exists("Figures")){
  dir.create("Figures")
}

### Create Tables Directory in Working Directory if Not Present
if(!dir.exists("Tables")){
  dir.create("Tables")
}


#######################################################
## Load Cleaned/Aggregated SLES Data and State Covariates
####################################################

#### Individual Level File - 1 row per legislator x legislative term
sles <- read_csv("Data/SLES_individual_level.csv", col_types = cols(district = 'c')) %>%
  ## For Tables A11/12: re-scaling SLES Component Variables to Mean 1
  group_by(state, chamber, term) %>%
  mutate(BILL_wshare_rescaled = BILL_wshare * n(),
         AIC_wshare_rescaled = AIC_wshare * n(),
         ABC_wshare_rescaled = ABC_wshare * n(),
         PASS_wshare_rescaled = PASS_wshare * n(),
         LAW_wshare_rescaled = LAW_wshare * n()) %>%
  ungroup() 

glimpse(sles)

#### State Level File - 1 row per state x legislative term
sles_agg <- read_csv("Data/SLES_state_level.csv", col_types = cols())
glimpse(sles_agg)


###########################
#### Table A1: List of States and Years
###############################

#### Identify State_Years for which we have LES
table_a1 <- sles %>%
  rename(State = state) %>%
  group_by(State) %>%
  summarize(`Years with SLES` = paste0(min(term_start), '-', max(term_end)),
            `Number of Unique Legislators` = length(unique(sles_id)),
            `Number of Unique Scores` = n(),
            .groups = 'drop') %>%
  add_row(State = "KS", `Years with SLES` = "None", `Number of Unique Legislators` = 0, `Number of Unique Scores` = 0) %>%
  arrange(State) %>%
  as_hux()

### Remove Scientific Notation
number_format(table_a1) <- 0

### Print Table
table_a1

### Export
huxtable::quick_docx(table_a1, file = "Tables/TableA1.docx", open = FALSE)

rm(table_a1)

######################################################################
##### Table A2: List of Newspapers Used to Identify S&S Bills
######################################################################

table_a2 <- data.frame(State = state.abb) %>% arrange(State)
fill_cols <- c("Newspaper(s)", "Newspaper Location", "State Capital")

table_a2[table_a2$State == "AK", fill_cols] <- c("Anchorage Daily News; Juneau Empire", "Anchorage, Juneau", "Juneau")
table_a2[table_a2$State == "AL", fill_cols] <- c("Birmingham News", "Birmingham", "Montgomery")
table_a2[table_a2$State == "AR", fill_cols] <- c("Arkansas Democrat-Gazette", "Little Rock", "Little Rock")
table_a2[table_a2$State == "AZ", fill_cols] <- c("Arizona Capital Times; Arizona Daily Star", "Phoenix, Tucson", "Phoenix")
table_a2[table_a2$State == "CA", fill_cols] <- c("Orange County Register", "OC (Irvine HQ)", "Sacramento")
table_a2[table_a2$State == "CO", fill_cols] <- c("Denver Post; Daily Camera", "Denver, Boulder", "Denver")
table_a2[table_a2$State == "CT", fill_cols] <- c("Hartford Courant", "Hartford", "Hartford")
table_a2[table_a2$State == "DE", fill_cols] <- c("Delaware State News", "Dover", "Dover")
table_a2[table_a2$State == "FL", fill_cols] <- c("Tampa Bay Times", "Tampa Bay", "Tallahassee")
table_a2[table_a2$State == "GA", fill_cols] <- c("Atlanta Journal-Constitution", "Atlanta", "Atlanta")
table_a2[table_a2$State == "HI", fill_cols] <- c("Honolulu Star Bulletin; Honolulu Star-Advertiser", "Honolulu", "Honolulu")
table_a2[table_a2$State == "IA", fill_cols] <- c("Telegraph Herald", "Dubuque", "Des Moines")
table_a2[table_a2$State == "ID", fill_cols] <- c("Idaho Business Review", "Boise", "Boise")
table_a2[table_a2$State == "IL", fill_cols] <- c("State Journal-Register", "Springfield", "Springfield")
table_a2[table_a2$State == "IN", fill_cols] <- c("Fort Wayne News-Sentinel", "Fort Wayne", "Indianapolis")
table_a2[table_a2$State == "KS", fill_cols] <- c("Topeka Capital Journal", "Topeka", "Topeka")
table_a2[table_a2$State == "KY", fill_cols] <- c("Lexington Herald-Leader", "Lexington", "Frankfort")
table_a2[table_a2$State == "LA", fill_cols] <- c("The Advocate", "Baton Rouge", "Baton Rouge")
table_a2[table_a2$State == "MA", fill_cols] <- c("Telegram and Gazette", "Worcester", "Boston")
table_a2[table_a2$State == "MD", fill_cols] <- c("The Capital", "Annapolis", "Annapolis")
table_a2[table_a2$State == "ME", fill_cols] <- c("Portland Press Herald", "Portland", "Augusta")
table_a2[table_a2$State == "MI", fill_cols] <- c("The Detroit News", "Detroit", "Lansing")
table_a2[table_a2$State == "MN", fill_cols] <- c("St. Paul Pioneer Press", "Saint Paul", "Saint Paul")
table_a2[table_a2$State == "MO", fill_cols] <- c("St. Louis Post-Dispatch", "Saint Louis", "Jefferson City")
table_a2[table_a2$State == "MS", fill_cols] <- c("Mississippi Sun Herald; Mississippi Business Journal", "Biloxi/Gulfport, Jackson", "Jackson")
table_a2[table_a2$State == "MT", fill_cols] <- c("Billings Gazette", "Billings", "Helena")
table_a2[table_a2$State == "NC", fill_cols] <- c("The News & Observer", "Raleigh", "Raleigh")
table_a2[table_a2$State == "ND", fill_cols] <- c("Bismarck Tribune", "Bismarck", "Bismarck")
table_a2[table_a2$State == "NE", fill_cols] <- c("Lincoln Journal Star", "Lincoln", "Lincoln")
table_a2[table_a2$State == "NH", fill_cols] <- c("New Hampshire Union", "Manchester", "Concord")
table_a2[table_a2$State == "NJ", fill_cols] <- c("The Press of Atlantic City", "Atlantic City", "Trenton")
table_a2[table_a2$State == "NM", fill_cols] <- c("Santa Fe New Mexican", "Santa Fe", "Santa Fe")
table_a2[table_a2$State == "NV", fill_cols] <- c("Las Vegas Review-Journal", "Las Vegas", "Carson City")
table_a2[table_a2$State == "NY", fill_cols] <- c("New York Times; New York Daily News", "New York City", "Albany")
table_a2[table_a2$State == "OH", fill_cols] <- c("Dayton Daily News", "Dayton", "Columbus")
table_a2[table_a2$State == "OK", fill_cols] <- c("Daily Oklahoman", "Oklahoma City", "Oklahoma City")
table_a2[table_a2$State == "OR", fill_cols] <- c("Daily Journal of Commerce", "Portland", "Salem")
table_a2[table_a2$State == "PA", fill_cols] <- c("Philadelphia Daily News; The Patriot-News", "Philadelphia, Harrisburg", "Harrisburg")
table_a2[table_a2$State == "RI", fill_cols] <- c("Providence Journal", "Providence", "Providence")
table_a2[table_a2$State == "SC", fill_cols] <- c("The Post & Courier", "Charleston", "Columbia")
table_a2[table_a2$State == "SD", fill_cols] <- c("The American News", "Aberdeen", "Pierre")
table_a2[table_a2$State == "TN", fill_cols] <- c("Chattanooga Times Free Press", "Chattanooga", "Nashville")
table_a2[table_a2$State == "TX", fill_cols] <- c("Austin American-Statesman", "Austin", "Austin")
table_a2[table_a2$State == "UT", fill_cols] <- c("Salt Lake City Deseret News", "Salt Lake City", "Salt Lake City")
table_a2[table_a2$State == "VA", fill_cols] <- c("Richmond Times Dispatch", "Richmond", "Richmond")
table_a2[table_a2$State == "VT", fill_cols] <- c("Brattleboro Reformer", "Brattleboro", "Montpelier")
table_a2[table_a2$State == "WA", fill_cols] <- c("Seattle Times; The Columbian", "Seattle, Vancouver", "Olympia")
table_a2[table_a2$State == "WI", fill_cols] <- c("Wisconsin State Journal", "Madison", "Madison")
table_a2[table_a2$State == "WV", fill_cols] <- c("Charleston Gazette-Journal", "Charleston", "Charleston")
table_a2[table_a2$State == "WY", fill_cols] <- c("Wyoming Tribune-Eagle", "Cheyenne", "Cheyenne")

### Print Table
table_a2

### Export
huxtable::quick_docx(table_a2, file = "Tables/TableA2.docx", open = FALSE)

rm(table_a2, fill_cols)

######################################################################
##### Table A3: Bill Types Retained by State
######################################################################

table_a3 <- data.frame(State = state.abb) %>% arrange(State)
table_a3[table_a3$State == "AK", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "AL", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "AR", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "AZ", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "CA", "Bill Prefixes"] <- paste(c("AB", "SB", "ABX", "SBX"), collapse = ", ")
table_a3[table_a3$State == "CO", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "CT", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "DE", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "FL", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "GA", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "HI", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "IA", "Bill Prefixes"] <- paste(c('HF', 'SF'), collapse = ", ")
table_a3[table_a3$State == "ID", "Bill Prefixes"] <- paste(c("H", "S"), collapse = ", ")
table_a3[table_a3$State == "IL", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "IN", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "KS", "Bill Prefixes"] <- paste(" --- ", collapse = ", ")
table_a3[table_a3$State == "KY", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "LA", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "MA", "Bill Prefixes"] <- paste(c('H', 'S'), collapse = ", ")
table_a3[table_a3$State == "MD", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "ME", "Bill Prefixes"] <- paste(c('HP', 'SP'), collapse = ", ")
table_a3[table_a3$State == "MI", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "MN", "Bill Prefixes"] <- paste(c('HF', 'SF'), collapse = ", ")
table_a3[table_a3$State == "MO", "Bill Prefixes"] <- paste(c('HB', "SB"), collapse = ", ")
table_a3[table_a3$State == "MS", "Bill Prefixes"] <- paste(c('HB', "SB"), collapse = ", ")
table_a3[table_a3$State == "MT", "Bill Prefixes"] <- paste(c('HB', "SB"), collapse = ", ")
table_a3[table_a3$State == "NC", "Bill Prefixes"] <- paste(c('H', 'S'), collapse = ", ")
table_a3[table_a3$State == "ND", "Bill Prefixes"] <- paste(c('HB', "SB"), collapse = ", ")
table_a3[table_a3$State == "NE", "Bill Prefixes"] <- "LB"
table_a3[table_a3$State == "NH", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "NJ", "Bill Prefixes"] <- paste(c("A", "S"), collapse = ", ")
table_a3[table_a3$State == "NM", "Bill Prefixes"] <- paste(c('HB', "SB"), collapse = ", ")
table_a3[table_a3$State == "NV", "Bill Prefixes"] <- paste(c("AB", "SB"), collapse = ", ")
table_a3[table_a3$State == "NY", "Bill Prefixes"] <- paste(c("A", "S"), collapse = ", ")
table_a3[table_a3$State == "OH", "Bill Prefixes"] <- paste(c("HB", "SB"), collapse = ", ")
table_a3[table_a3$State == "OK", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "OR", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "PA", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "RI", "Bill Prefixes"] <- paste(c('H', 'S'), collapse = ", ")
table_a3[table_a3$State == "SC", "Bill Prefixes"] <- paste(c('H', 'S'), collapse = ", ")
table_a3[table_a3$State == "SD", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "TN", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "TX", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "UT", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "VA", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "VT", "Bill Prefixes"] <- paste(c('H', "S"), collapse = ", ")
table_a3[table_a3$State == "WA", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "WI", "Bill Prefixes"] <- paste(c("AB", "SB"), collapse = ", ")
table_a3[table_a3$State == "WV", "Bill Prefixes"] <- paste(c('HB', 'SB'), collapse = ", ")
table_a3[table_a3$State == "WY", "Bill Prefixes"] <- paste(c('HB', "SF"), collapse = ", ")

### Additional Restrictions
table_a3[, "Additional Restrictions"] <- ""
table_a3[table_a3$State == "MA", "Additional Restrictions"] <- 'Legislation is labeled "bill" on the state webpage'
table_a3[table_a3$State == "ME", "Additional Restrictions"] <- 'Legislation has an LD number and is not titled "resolution"'
table_a3[table_a3$State == "MN", "Additional Restrictions"] <- 'Legislation is labeled "bill" on the state webpage'
table_a3[table_a3$State == "NC", "Additional Restrictions"] <- 'Legislation is labeled "bill" on the state webpage'
table_a3[table_a3$State == "RI", "Additional Restrictions"] <- 'Legislation is labeled "an act" on the state webpage'
table_a3[table_a3$State == "SC", "Additional Restrictions"] <- 'Legislation is not labeled "resolution" on the state webpage'
table_a3[table_a3$State == "WA", "Additional Restrictions"] <- 'Legislation is labeled "bill" on the state webpage'

### Print Table
table_a3

### Export
huxtable::quick_docx(table_a3, file = "Tables/TableA3.docx", open = FALSE)

rm(table_a3)


######################################################################
##### Table A4: Evaluating the Explanatory Power of Effectiveness Measures in North Carolina
######################################################################


### See `SLES_analysis_nccppr.R` script


###############################################################
##### Table A5: Individual-Level Summary Statistics + Data Sources
###############################################################

### Get Mean/SD for All Individual Level Variables
table_a5 <- sles %>%
  mutate(afam = ifelse(pred.race == "black", 1, 0),
         hispanic = ifelse(pred.race == "hispanic", 1, 0)) %>%
  select(SLES, seniority, comm_chair, in_majority, 
         Leader_majleader, Leader_minleader, Leader_speakerpres, power_comm, 
         ideo_med_distance, female, afam, hispanic, vote_share) %>%
  summarise_all(list(~mean(., na.rm=T), ~sd(., na.rm=T))) %>%
  gather('mean_variable', 'mean', ends_with('_mean')) %>%
  gather('sd_variable', 'sd', ends_with('_sd')) %>%
  mutate(var = gsub('_mean', '', mean_variable)) %>%
  filter(var == gsub('_sd', '', sd_variable)) %>%
  mutate(Description = NA, Sources = NA) %>%
  select(var, Description, mean, sd, Sources) %>%
  rename(Mean = mean, `Std. Dev.` = sd) %>% 
  as.data.frame() 

### Fill in Descriptions
table_a5$`Independent Variable` <- c('SLES', 'Seniority', "Committee Chair", "Majority Party",
                                        "Majority-Party Leadership", "Minority-Party Leadership", "Speaker/President", "Power Committee", 
                                        "Distance from Median", "Female", "African-American", "Latino", "Vote Share")

table_a5[table_a5$var == 'SLES',]$Description <- 'State legislative effectiveness score'
table_a5[table_a5$var == 'seniority',]$Description <- 'Number of consecutive terms served by member in chamber'
table_a5[table_a5$var == 'comm_chair',]$Description <- 'Equals "1" if member is a committee chair'
table_a5[table_a5$var == 'in_majority',]$Description <- 'Equals "1" if member is in majority party'
table_a5[table_a5$var == 'Leader_majleader',]$Description <- 'Equals "1" if member is the majority-party leader'
table_a5[table_a5$var == 'Leader_minleader',]$Description <- 'Equals "1" if member is the minority-party leader'
table_a5[table_a5$var == 'Leader_speakerpres',]$Description <- 'Equals "1" if member is Speaker or President of the chamber'
table_a5[table_a5$var == 'power_comm',]$Description <- 'Equals "1" if member serves on a committee related to the budget, finance, appropriations, or rules'
table_a5[table_a5$var == 'ideo_med_distance',]$Description <- "|Member i's Shor-McCarty ideology score - Median member's ideology score|"
table_a5[table_a5$var == 'female',]$Description <- 'Equals "1" if member is female'
table_a5[table_a5$var == 'afam',]$Description <- 'Equals "1" if member is African American'
table_a5[table_a5$var == 'hispanic',]$Description <- 'Equals "1" if member is Latino/Latina'
table_a5[table_a5$var == 'vote_share',]$Description <- 'Percentage of vote received in previous election'

#### Fill In Sources
table_a5[table_a5$var %in% c("SLES"),]$Sources <- "Constructed by authors"
table_a5[table_a5$var %in% c("seniority"),]$Sources <- "Constructed by authors in tandem with data from Klarner (2018)"
table_a5[table_a5$var %in% c("comm_chair"),]$Sources <- "Fouirnaies (2018); Fouirnaies and Hall (2018); State Legislative Webpages"
table_a5[table_a5$var %in% c("in_majority"),]$Sources <- "Constructed by authors in tandem with data from Klarner (2018)"
table_a5[table_a5$var %in% c("Leader_majleader", "Leader_minleader", "Leader_speakerpres"),]$Sources <- "Fouirnaies (2018); State Legisaltive Webpages"
table_a5[table_a5$var %in% c("power_comm"),]$Sources <- "Fouirnaies and Hall (2018); State Legisaltive Webpages"
table_a5[table_a5$var %in% c("ideo_med_distance"),]$Sources <- "Shor and McCarty (2011)"
table_a5[table_a5$var %in% c("female"),]$Sources <- "Center for American Women and Politics Women Elected Officials Database"
table_a5[table_a5$var %in% c("afam", "hispanic"),]$Sources <- "Estimated by authors using methods from Imai and Khanna (2016)"
table_a5[table_a5$var %in% c("vote_share"),]$Sources <- "Klarner (2018)"

### Format + Save Table
table_a5$Mean <- sprintf("%.3f", round(table_a5$Mean, 3))
table_a5$`Std. Dev.` <- round(table_a5$`Std. Dev.`, 3)

### Print Table
stargazer(select(table_a5, `Independent Variable`, Description, Mean, `Std. Dev.`, Sources), 
          type = 'text',
          summary = FALSE, 
          rownames = FALSE)

### Export Table
stargazer(select(table_a5, `Independent Variable`, Description, Mean, `Std. Dev.`, Sources), 
          type = 'html', 
          out = 'Tables/TableA5.doc',
          summary = FALSE, 
          rownames = FALSE)


rm(table_a5)

###############################################################
##### Table A6: State-Level Summary Statistics + Data Sources
###############################################################

### Get Mean/SD for All Individual Level Variables
table_a6 <- sles_agg %>%
  select(
    maj_min_SLES_meddiff,
    maj_above_min_SLES_median,
    chair_SLES_meddiff,
    majfrosh_SLES_meddiff,
    minfrosh_SLES_meddiff,
    leg_salary_yr_avg, slength_avg_all, ncsl_num_staff_per_leg, squire_index,
    aj_anymaj_setscal, aj_comm_gatekeep, aj_chamb_vote_comm_appts,
    num_chamber_comms, chamber_size, term_limits_enacted,
    SM_median_diff, SM_maj_sd, SM_min_sd, partisan_seatshare_diff, unified_gov
  ) %>%
  rename(SM_sd_maj = SM_maj_sd, SM_sd_min = SM_min_sd) %>%
  summarise_all(list(~mean(., na.rm=T), ~sd(., na.rm=T))) %>%
  gather('mean_variable', 'mean', ends_with('_mean')) %>%
  gather('sd_variable', 'sd', ends_with('_sd')) %>%
  mutate(var = gsub('_mean$', '', mean_variable)) %>%
  filter(var == gsub('_sd$', '', sd_variable)) %>%
  mutate(Description = NA, Sources = NA) %>%
  select(var, Description, mean, sd, Sources) %>%
  rename(Mean = mean, `Std. Dev.` = sd) %>% 
  as.data.frame() 

### Fill in Descriptions
table_a6$`Independent Variable` <- c(
  "SLES Partisan Difference", 
  "Share More Effective", 
  "SLES Chair Difference", 
  "Majority SLES Seniority Difference", 
  "Minority SLES Seniority Difference",
  "Annual Legislative Salary", 
  "Session Length", 
  "Staff per Legislator", 
  "Squire Index",
  "Majority Party Sets Calendar", 
  "Committee Gatekeeping Power", 
  "Chamber Votes on Committee Appointments",
  "Number of Committees", 
  "Chamber Size", 
  "Term Limits", 
  "Polarization", 
  "Majority Party Heterogeneity", 
  "Minority Party Heterogeneity",
  "Partisan Seat Share Imbalance", 
  "Unified Government")

table_a6[table_a6$var == 'maj_min_SLES_meddiff',]$Description <- 'Median SLES among majority-party members minus that among minority-party members in the chamber'
table_a6[table_a6$var == 'maj_above_min_SLES_median',]$Description <- 'Proportion of majority-party members with SLES above minority-party median SLES in the chamber'
table_a6[table_a6$var == 'chair_SLES_meddiff',]$Description <- 'Median SLES among committee chairs minus that among rank-and-file members in the chamber'
table_a6[table_a6$var == 'majfrosh_SLES_meddiff',]$Description <- 'Median SLES among majority-party senior members minus that among majority-party freshmen in the chamber'
table_a6[table_a6$var == 'minfrosh_SLES_meddiff',]$Description <- 'Median SLES among minority-party senior members minus that among minority-party freshmen members in the chamber'
table_a6[table_a6$var == 'leg_salary_yr_avg',]$Description <- 'Average yearly salary excluding per diem for state legislative service'
table_a6[table_a6$var == 'slength_avg_all',]$Description <- 'Average yearly length of legislative sessions (including specials)'
table_a6[table_a6$var == 'ncsl_num_staff_per_leg',]$Description <- 'Average Number of legislative staff per state legislator'
table_a6[table_a6$var == 'squire_index',]$Description <- 'Squire index of legisaltive professionalism'
table_a6[table_a6$var == "aj_anymaj_setscal",]$Description <- 'Equals "1" if majority party leadership and/or majority party-controlled committees have power over the legislative calendar'
table_a6[table_a6$var == "aj_comm_gatekeep",]$Description <- 'Equals "1" if majority party-controlled committees have the power to deny a bill a hearing and/or not report it to floor'
table_a6[table_a6$var == "aj_chamb_vote_comm_appts",]$Description <- 'Equals "1" if the full chamber membership votes on committee appointments'
table_a6[table_a6$var == 'num_chamber_comms',]$Description <- 'Number of standing committees'
table_a6[table_a6$var == 'chamber_size',]$Description <- 'Number of seats in a legislative chamber'
table_a6[table_a6$var == 'term_limits_enacted',]$Description <- 'Equals "1" if a state has adopted term limits for state legislators'
table_a6[table_a6$var == "SM_median_diff",]$Description <- 'Absolute difference in median Shor-McCarty ideology scores between parties'
table_a6[table_a6$var == "SM_sd_maj",]$Description <- "Standard deviation of majority party's Shor-McCarty ideology scores"
table_a6[table_a6$var == "SM_sd_min",]$Description <- "Standard deviation of minority party's Shor-McCarty ideology scores"
table_a6[table_a6$var == 'partisan_seatshare_diff',]$Description <- 'Absolute difference in share of seats controlled by each party'
table_a6[table_a6$var == 'unified_gov',]$Description <- "Majority party controls all legislative chambers and governor's office"

#### Fill In Sources
table_a6[table_a6$var %in% c('maj_min_SLES_meddiff', 'maj_above_min_SLES_median', 'chair_SLES_meddiff', 'majfrosh_SLES_meddiff', 'minfrosh_SLES_meddiff'),]$Sources <- "Constructed by Authors"
table_a6[table_a6$var %in% c('leg_salary_yr_avg', 'slength_avg_all'),]$Sources <- 'Bowen and Greene (2014); The Book of the States (2014-2018)'
table_a6[table_a6$var %in% c('ncsl_num_staff_per_leg', 'term_limits_enacted'),]$Sources <- 'National Conference of State Legislatures' # https://www.ncsl.org/research/about-state-legislatures/chart-of-term-limits-states.aspx
table_a6[table_a6$var == 'squire_index',]$Sources <- "Squire (2012); Squire (2017)"
table_a6[table_a6$var %in% c("aj_comm_gatekeep", "aj_anymaj_setscal", "aj_chamb_vote_comm_appts"),]$Sources <- 'Anzia and Jackman (2013)'
table_a6[table_a6$var %in% c("num_chamber_comms"),]$Sources <- 'The Book of the States (1987-2018)'
table_a6[table_a6$var == 'chamber_size',]$Sources <- 'Klarner (2013)' # https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/LZHMG3
table_a6[table_a6$var %in% c("SM_median_diff", "SM_sd_maj", "SM_sd_min"),]$Sources <- 'Shor and McCarty (2011)'
table_a6[table_a6$var %in% c('partisan_seatshare_diff', 'unified_gov'),]$Sources <- 'Constructed by authors in tandem with data from Klarner (2013)'

### Adjust + Save Table
table_a6$Mean <- round(table_a6$Mean, 3)
table_a6$`Std. Dev.` <- round(table_a6$`Std. Dev.`, 3)

### Print Table
stargazer(select(table_a6, `Independent Variable`, Description, Mean, `Std. Dev.`, Sources), 
          type = 'text',
          summary = FALSE, 
          rownames = FALSE)

### Export Table
stargazer(select(table_a6, `Independent Variable`, Description, Mean, `Std. Dev.`, Sources), 
          type = 'html',
          out = 'Tables/TableA6.doc',
          summary = FALSE, 
          rownames = FALSE)

rm(table_a6)


######################################################################
##### Table A7: Replication to Seniority Coded Based on Total Prior Legislative Service
######################################################################

### Using Total Terms Served for Combined, Lower Chamber, Upper Chamber
tserved_model <- lm(SLES ~ terms_served + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
tserved_model_H <- lm(SLES ~ terms_served + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state) + factor(biennial_grps), data = sles, subset = sles$chamber == "lower")
tserved_model_S <- lm(SLES ~ terms_served + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state) + factor(biennial_grps), data = sles, subset = sles$chamber == "upper")

### Using Chamber-Specific Terms Served for Lower Chamber and Upper Chamber
tservedinH_model_H <- lm(SLES ~ terms_served_same_chamber + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state) + factor(biennial_grps), 
                         data = sles %>% rename(terms_served_same_chamber = terms_served_lower), 
                         subset = sles$chamber == "lower")
tservedinS_model_S <- lm(SLES ~ terms_served_same_chamber + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state) + factor(biennial_grps), 
                         data = sles %>% rename(terms_served_same_chamber = terms_served_upper), 
                         subset = sles$chamber == "upper")

### Clustering SE's for use with Stargazer
tserved_SEs <- list(starprep(tserved_model, clusters = sles[-tserved_model$na.action,]$sles_id, se_type = "stata")[[1]], 
                    starprep(tserved_model_H, clusters = sles[sles$chamber == "lower",][-tserved_model_H$na.action,]$sles_id, se_type = "stata")[[1]],
                    starprep(tservedinH_model_H, clusters = sles[sles$chamber == "lower",][-tservedinH_model_H$na.action,]$sles_id, se_type = "stata")[[1]],
                    starprep(tserved_model_S, clusters = sles[sles$chamber == "upper",][-tserved_model_S$na.action,]$sles_id, se_type = "stata")[[1]],
                    starprep(tservedinS_model_S, clusters = sles[sles$chamber == "upper",][-tservedinS_model_S$na.action,]$sles_id, se_type = "stata")[[1]])

### Export Table A7
stargazer::stargazer(tserved_model, tserved_model_H, tservedinH_model_H, tserved_model_S, tservedinS_model_S, 
                     se = tserved_SEs,
                     type = "text",
                     #type = "html", out = "Tables/TableA7.doc",
                     dep.var.caption = "Dependent variable: SLES",
                     dep.var.labels.include = FALSE,
                     omit = c("state", "biennial"), omit.stat = c("f", 'ser', 'adj.rsq'),
                     column.labels = c("Full Sample", "Lower Chambers", "Upper Chambers"), 
                     column.separate = c(1, 2, 2),
                     model.numbers = TRUE,
                     #column.labels = c("(A7.1)", "(A7.2)", "(A7.3)", "(A7.4)", "(A7.5)"), # Update these manually
                     order = c("terms_served", "chair", "in_majority", "majleader", "minleader", "speakerpres", "in_maj"),
                     covariate.labels =  c("Terms Served - Total",
                                           "Terms Served - Same Chamber",
                                           #"Terms Served - Lower Chambers", "Terms Served - Upper Chambers",
                                           "Committee Chair", "Majority Party", "Majority Leadership", "Minority Leadership",
                                           "Speaker/President", "Power Committee", "Distance from Median", "Female",
                                           "African-American", "Hispanic", "Vote Share", "Vote Share Squared", "Constant"),
                     star.char = c("+", "*", "**"),
                     # add.lines = list(c("State-Chamber FE", "Yes", "No", "No", "No", "No"),
                     #                  c("State FE", "No", "Yes", "Yes", "Yes", "Yes"),
                     #                  c("Term FE", rep("Yes", 5))),
                     notes = "+p<0.1; *p<0.05; **p<0.01", notes.append = FALSE)

rm(tserved_model, tserved_model_H, tservedinH_model_H, tserved_model_S, tservedinS_model_S, 
   tserved_SEs)

######################################################################
##### Table A8: Replication to Further Normalization of Scores across States
######################################################################

###### Normalized Version for Appendix
z_model <- lm(SLES_z ~ seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
z_model_H <- lm(SLES_z ~ seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state) + factor(biennial_grps), data = sles, subset = sles$chamber == "lower")
z_model_S <- lm(SLES_z ~ seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state) + factor(biennial_grps), data = sles, subset = sles$chamber == "upper")

### Clustering SE's for use with Stargazer
z_SEs <- list(starprep(z_model, clusters = sles[-z_model$na.action,]$sles_id, se_type = "stata")[[1]],
              starprep(z_model_H, clusters = sles[sles$chamber == "lower",][-z_model_H$na.action,]$sles_id, se_type = "stata")[[1]],
              starprep(z_model_S, clusters = sles[sles$chamber == "upper",][-z_model_S$na.action,]$sles_id, se_type = "stata")[[1]])

### Export Table A8
stargazer::stargazer(z_model, z_model_H, z_model_S, se = z_SEs,
                     type = "text",
                     #type = "html", out = "Tables/TableA8.doc",
                     dep.var.caption = "Dependent variable: Normalized SLES",
                     dep.var.labels.include = FALSE,
                     column.labels = c("Full Sample (A8.1)", "Lower Chambers (A8.2)", "Upper Chambers (A8.3)"), model.numbers = FALSE,
                     omit = c("state", "biennial"),
                     omit.stat = c("f", 'ser', 'adj.rsq'),
                     order = c("seniority", "chair", "in_majority", "majleader", "minleader", "speakerpres", "in_maj"),
                     covariate.labels = c('Seniority', "Committee Chair", "Majority Party", "Majority Leadership", "Minority Leadership",
                                          "Speaker/President", "Power Committee", "Distance from Median", "Female",
                                          "African-American", "Hispanic", "Vote Share", "Vote Share\\^2", "Constant"),
                     # add.lines = list(c("State-Chamber FE", "Yes", "No", "No"),
                     #                  c("State FE", "No", "Yes", "Yes"),
                     #                  c("Term FE", rep("Yes", 3))),
                     star.char = c("+", "*", "**"),
                     notes = "+p<0.1; *p<0.05; **p<0.01", notes.append = FALSE)

rm(z_model, z_model_H, z_model_S, z_SEs)


######################################################################
##### Table A9: Replication of Institutional Effects Including Binding Term Limits
######################################################################

mod_majmin_tlb <- lm(maj_min_SLES_meddiff ~ log(leg_salary_yr_avg + 1) + log(slength_avg_all) + ncsl_num_staff_per_leg + aj_anymaj_setscal + aj_comm_gatekeep + aj_chamb_vote_comm_appts + num_chamber_comms + log(chamber_size) + term_limits_applied + SM_median_diff + SM_maj_sd + SM_min_sd + partisan_seatshare_diff + unified_gov, data = sles_agg) 
mod_chair_tlb <- lm(chair_SLES_meddiff ~ log(leg_salary_yr_avg + 1) + log(slength_avg_all) + ncsl_num_staff_per_leg + aj_anymaj_setscal + aj_comm_gatekeep + aj_chamb_vote_comm_appts + num_chamber_comms + log(chamber_size) + term_limits_applied + SM_median_diff + SM_maj_sd + SM_min_sd + partisan_seatshare_diff + unified_gov, data = sles_agg) 
mod_majsenior_tlb <- lm(majfrosh_SLES_meddiff ~ log(leg_salary_yr_avg + 1) + log(slength_avg_all) + ncsl_num_staff_per_leg + aj_anymaj_setscal + aj_comm_gatekeep + aj_chamb_vote_comm_appts + num_chamber_comms + log(chamber_size) + term_limits_applied + SM_median_diff + SM_maj_sd + SM_min_sd + partisan_seatshare_diff + unified_gov, data = sles_agg) 
mod_minsenior_tlb <- lm(minfrosh_SLES_meddiff ~ log(leg_salary_yr_avg + 1) + log(slength_avg_all) + ncsl_num_staff_per_leg + aj_anymaj_setscal + aj_comm_gatekeep + aj_chamb_vote_comm_appts + num_chamber_comms + log(chamber_size) + term_limits_applied + SM_median_diff + SM_maj_sd + SM_min_sd + partisan_seatshare_diff + unified_gov, data = sles_agg) 

mm_SEs_tlb <- list(
  starprep(mod_majmin_tlb, clusters = sles_agg[-mod_majmin_tlb$na.action,]$state_chamber, se_type = "stata")[[1]],
  starprep(mod_chair_tlb, clusters = sles_agg[-mod_chair_tlb$na.action,]$state_chamber, se_type = "stata")[[1]],
  starprep(mod_majsenior_tlb, clusters = sles_agg[-mod_majsenior_tlb$na.action,]$state_chamber, se_type = "stata")[[1]],
  starprep(mod_minsenior_tlb, clusters = sles_agg[-mod_minsenior_tlb$na.action,]$state_chamber, se_type = "stata")[[1]]
)

### Table A9
stargazer(mod_majmin_tlb, mod_chair_tlb, mod_majsenior_tlb, mod_minsenior_tlb,
          se = mm_SEs_tlb, 
          omit.stat = c("f", "ser"),
          type = "text",
          #type = "html", out = "Tables/TableA9.doc",
          dep.var.caption = "Dependent variable: SLES",
          dep.var.labels.include = FALSE,
          column.labels = c("SLES Partisan Difference (A9.1)", 
                            "SLES Chair Difference (A9.2)", 
                            "Majority SLES Seniority Difference (A9.3)", 
                            "Minority SLES Seniority Difference (A9.4)"),
          model.numbers = FALSE,
          covariate.labels = c("Log Annual Salary", "Log Session Length", "Staff per Legislator",
                               "Majority Party Controls Calendar", "Committee Gatekeeping Power", "Chamber Votes on Committee Appointments",
                               "Number of Committees", "Log Chamber Size", "Term Limits (Binding Date)",
                               "Polarization", "Majority Party Heterogeneity", "Minority Party Heterogeneity",
                               "Partisan Seat Share Imbalance", "Unified Government", 
                               "Constant"),
          star.char = c("+", "*", "**"),
          notes = "+p<0.1; *p<0.05; **p<0.01", 
          notes.append = FALSE)

rm(mod_majmin_tlb, mod_chair_tlb, mod_majsenior_tlb, mod_minsenior_tlb, mm_SEs_tlb)



######################################################################
##### Table A10: Replication of Institutional Effects on Individual-Level Data
######################################################################

### Models 
slength_maj_model <- lm(SLES ~ in_majority*log(slength_avg_all) + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
slength_chair_model <- lm(SLES ~ comm_chair*log(slength_avg_all) + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
csize_chair_model <- lm(SLES ~ comm_chair*log(chamber_size) + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
csize_senior_model <- lm(SLES ~ seniority*log(chamber_size) + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)

### Clustering SE's for use with Stargazer
supp_SEs <- list(starprep(slength_maj_model, clusters = sles[-slength_maj_model$na.action,]$sles_id, se_type = "stata")[[1]], 
                 starprep(slength_chair_model, clusters = sles[-slength_chair_model$na.action,]$sles_id, se_type = "stata")[[1]],
                 starprep(csize_chair_model, clusters = sles[-csize_chair_model$na.action,]$sles_id, se_type = "stata")[[1]],
                 starprep(csize_senior_model, clusters = sles[-csize_senior_model$na.action,]$sles_id, se_type = "stata")[[1]])

### Table 1 - Determinants of SLES
stargazer::stargazer(slength_maj_model, slength_chair_model, csize_chair_model, csize_senior_model,
                     se = supp_SEs,
                     type = "text", 
                     #type = "html", out = "Tables/TableA10.doc",
                     dep.var.caption = "Dependent variable: SLES",
                     dep.var.labels.include = FALSE,
                     column.labels = c("(A10.1)", "(A10.2)", "(A10.3)", "(A10.4)"), model.numbers = FALSE,
                     omit = c("state", "biennial"),
                     omit.stat = c("f", 'ser', 'adj.rsq'),
                     order = c( 
                       "slength_avg_all", "in_majority:log(slength_avg_all)", "comm_chair:log(slength_avg_all)", 
                       "chamber_size", "comm_chair:log(chamber_size)", "seniority:log(chamber_size)", 
                       "seniority", "chair", "in_majority", "majleader", "minleader", "speakerpres", "in_maj"),
                     covariate.labels = c(
                       "Log Session Length", "Log Session Length * Majority Party", "Log Session Length * Committee Chair",
                       "Log Chamber Size", "Log Chamber Size * Committee Chair", "Log Chamber Size * Seniority",
                       'Seniority', "Committee Chair", "Majority Party", "Majority Leadership", "Minority Leadership",
                       "Speaker/President", "Power Committee", "Distance from Median", "Female",
                       "African-American", "Hispanic", "Vote Share", "Vote Share\\^2", "Constant"),
                     # add.lines = list(c("State-Chamber FE", rep("Yes", 4)), 
                     #                  c("Term FE", rep("Yes", 4))),
                     star.char = c("+", "*", "**"),
                     notes = "+p<0.1; *p<0.05; **p<0.01", notes.append = FALSE)

rm(slength_maj_model, slength_chair_model, csize_chair_model, csize_senior_model, supp_SEs)


######################################################################
##### Table A11: Importance of Calendar Control across Legislative Stages
######################################################################

### Models 
bill_mod <- lm(BILL_wshare_rescaled ~ in_majority*aj_anymaj_setscal + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
aic_mod <- lm(AIC_wshare_rescaled ~ in_majority*aj_anymaj_setscal + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
abc_mod <- lm(ABC_wshare_rescaled ~ in_majority*aj_anymaj_setscal + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
pass_mod <- lm(PASS_wshare_rescaled ~ in_majority*aj_anymaj_setscal + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
law_mod <- lm(LAW_wshare_rescaled ~ in_majority*aj_anymaj_setscal + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)

### Clustering SE's for use with Stargazer
stage_SEs <- list(starprep(bill_mod, clusters = sles[-bill_mod$na.action,]$sles_id, se_type = "stata")[[1]], 
                  starprep(aic_mod, clusters = sles[-aic_mod$na.action,]$sles_id, se_type = "stata")[[1]],
                  starprep(abc_mod, clusters = sles[-abc_mod$na.action,]$sles_id, se_type = "stata")[[1]],
                  starprep(pass_mod, clusters = sles[-pass_mod$na.action,]$sles_id, se_type = "stata")[[1]],
                  starprep(law_mod, clusters = sles[-law_mod$na.action,]$sles_id, se_type = "stata")[[1]])

### Export Table A11
stargazer::stargazer(bill_mod, aic_mod, abc_mod, pass_mod, law_mod,
                     se = stage_SEs,
                     type = "text", 
                     #type = "html", out = "Tables/TableA11.doc",
                     dep.var.caption = "Dependent variable: ",
                     dep.var.labels.include = FALSE,
                     column.labels = c("BILL Score (A11.1)",
                                      "AIC Score (A11.2)",
                                      "ABC Score (A11.3)",
                                      "PASS Score (A11.4)",
                                      "LAW Score (A11.5)"),
                     model.numbers = FALSE,
                     omit = c("state", "biennial"),
                     omit.stat = c("f", 'ser', 'adj.rsq'),
                     order = c( 
                       "in_majority:aj_anymaj_setscal", "aj_anymaj", "in_majority", 
                       "seniority", "chair", "majleader", "minleader", "speakerpres", "in_maj"),
                     covariate.labels = c(
                       "Majority Party * Majority Party Controls Calendar",
                       "Majority Party Controls Calendar",
                       "Majority Party",
                       'Seniority', "Committee Chair",
                       "Majority Leadership", "Minority Leadership", "Speaker/President",
                       "Power Committee",  "Distance from Median", "Female",
                       "African-American", "Hispanic", "Vote Share", "Vote Share\\^2", "Constant"),
                     # add.lines = list(c("State-Chamber FE", rep("Yes", 5)), 
                     #                  c("Term FE", rep("Yes", 5))),
                     star.char = c("+", "*", "**"),
                     notes = "+p<0.1; *p<0.05; **p<0.01", notes.append = FALSE)

rm(bill_mod, aic_mod, abc_mod, pass_mod, law_mod, stage_SEs)


######################################################################
##### Table A12: Importance of Gatekeeping Powers across Legislative Stages
######################################################################

### Models
bill_gk_mod <- lm(BILL_wshare_rescaled ~ in_majority*aj_comm_gatekeep + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
aic_gk_mod <- lm(AIC_wshare_rescaled ~ in_majority*aj_comm_gatekeep + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
abc_gk_mod <- lm(ABC_wshare_rescaled ~ in_majority*aj_comm_gatekeep + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
pass_gk_mod <- lm(PASS_wshare_rescaled ~ in_majority*aj_comm_gatekeep + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)
law_gk_mod <- lm(LAW_wshare_rescaled ~ in_majority*aj_comm_gatekeep + seniority + comm_chair + in_majority + Leader_majleader + Leader_minleader + Leader_speakerpres + power_comm + ideo_med_distance + female + I(pred.race == "black") + I(pred.race == "hispanic") + vote_share + I(vote_share^2) + factor(state_chamber) + factor(biennial_grps), data = sles)

### Clustering SE's for use with Stargazer
gk_SEs <- list(starprep(bill_gk_mod, clusters = sles[-bill_gk_mod$na.action,]$sles_id, se_type = "stata")[[1]], 
               starprep(aic_gk_mod, clusters = sles[-aic_gk_mod$na.action,]$sles_id, se_type = "stata")[[1]],
               starprep(abc_gk_mod, clusters = sles[-abc_gk_mod$na.action,]$sles_id, se_type = "stata")[[1]],
               starprep(pass_gk_mod, clusters = sles[-pass_gk_mod$na.action,]$sles_id, se_type = "stata")[[1]],
               starprep(law_gk_mod, clusters = sles[-law_gk_mod$na.action,]$sles_id, se_type = "stata")[[1]])

### Export Table A12
stargazer::stargazer(bill_gk_mod, aic_gk_mod, abc_gk_mod, pass_gk_mod, law_gk_mod,
                     se = gk_SEs,
                     type = "text", 
                     #type = "html", out = "Tables/TableA12.doc",
                     dep.var.caption = "Dependent variable: ",
                     dep.var.labels.include = FALSE,
                     column.labels = c("BILL Score (A12.1)",
                                       "AIC Score (A12.2)",
                                       "ABC Score (A12.3)",
                                       "PASS Score (A12.4)",
                                       "LAW Score (A12.5)"),
                     omit = c("state", "biennial"),
                     omit.stat = c("f", 'ser', 'adj.rsq'),
                     order = c( 
                       "in_majority:aj_comm_gatekeep", "aj_comm_gatekeep", "in_majority", 
                       "seniority", "chair", "majleader", "minleader", "speakerpres", "in_maj"),
                     covariate.labels = c(
                       "Majority Party * Committee Gatekeeping Power",
                       "Committee Gatekeeping Power",
                       "Majority Party",
                       'Seniority', "Committee Chair",
                       "Majority Leadership", "Minority Leadership", "Speaker/President",
                       "Power Committee",  "Distance from Median", "Female",
                       "African-American", "Hispanic", "Vote Share", "Vote Share\\^2", "Constant"),
                     # add.lines = list(c("State-Chamber FE", rep("Yes", 5)), 
                     #                  c("Term FE", rep("Yes", 5))),
                     star.char = c("+", "*", "**"),
                     notes = "+p<0.1; *p<0.05; **p<0.01", notes.append = FALSE)

rm(bill_gk_mod, aic_gk_mod, abc_gk_mod, pass_gk_mod, law_gk_mod, gk_SEs)


######################################################################
##### Table A13: Commemorative Coding Term
######################################################################

vw_commem <- c("expressing support", "urging", "condol", "commemorat", " honor|^honor", "memoria", 
               "congratul", "public holiday", "for the relief of", "for the private relief of", "retention of the name", 
               "medal", "posthumous", "provide for correction", "to name", 'rename', "to remove any doubt")
additions <- c("anniversary", "raise awareness", 'awareness (day|week|month)', "dedicating", "celebrat", 'appreciat',
               " commend|^commend", 'official design', 'official emblem', 'remembrance', 'state symbol', 'proclamation')
exclude <- c('appropriates', 'appropriation', 'approp\\.', 'appropriating', 'to appropriate', '\\$', 'dollars', 'to fund', 'funding', 'funds',
             'expenditure', 'penalt', 'felony', 'memorial (act|law)', 'criminal', 'lien', 'statutory', 'license fee', 
             '^tax| tax', 'prohibit', 'rainy day', 'procedure', 'contract', 'firearm', 'weapon', 'inflation',
             'exempt', 'legislative intent', 'deposit', 'budget', 'tuition', 'violation', 'compensation', 'promulgate',
             'regulation', 'bonds', 'jurisdiction', 'liabilit', 'task force', 'annuity', 'probate', 'financ',
             'honor[a-z]+ discharge', 'revenue', 'compliance', 'sale of', 'health benefit', 'insurer', 'primary care', 
             'grant program', 'purchase', 'donation', 'official language', 'refund', 'election', 'capital improvements',
             'liquor sales')

#### Construct Table
table_a13 <- tibble(`Terms from Volden and Wiseman (2014)` = paste0(vw_commem, collapse = "; "),
                    `Additional Terms` = paste0(additions, collapse = "; "),
                    `Excluded Terms` = paste0(exclude, collapse = "; ")) %>%
  t() %>%
  huxtable(add_rownames = TRUE) %>%
  as_hux() %>%
  set_width(1) %>%
  set_wrap(TRUE)

### Print Table
table_a13

### Export
huxtable::quick_docx(table_a13, file = "Tables/TableA13.docx", open = FALSE)

rm(table_a13, vw_commem, additions, exclude)



